package controllers;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.h2.result.ResultRemote;
import org.tempuri.InJsonData;
import org.tempuri.ReturnResult;
import org.tempuri.WebServiceHelpUtil;

import play.Logger;
import play.cache.Cache;
import play.mvc.Http.Request;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import config.ActionConfig;
import config.AppConfig;
import config.SessionConfig;
import utils.HelpUtils;
import models.JSONReqException;
import models.Menu;
import models.Result;
import models.User;

public class IndexController extends BaseController {
	
	public static void index(String menuId){
		renderArgs.put("menuId", menuId);
		renderArgs.put("menuEntranceID", "");
		render();
	}
	
	/**
	 * 得到菜单
	 */
	public static void getMenuList(){
		User user = getLoginUser();
		Result result  = (Result) Cache.get(user.getUsercode() + "_"+ session.getId());
		if(result!=null){
			renderJSON(result);
		}
		String jsonData = InJsonData.getRequsetJsonData(AppConfig.HANDLE_TYPE, AppConfig.PLAT_GETMODELDETAIL, user.getUsercode(), AppConfig.CURRENT_PLAT, user.getDbName(), request.remoteAddress, "");
		result  = new Result();
		try {
			ReturnResult returnResult = WebServiceHelpUtil.execute(jsonData);
			if(returnResult.isSuccess()){
				result.setCode(AppConfig.SUCCESS);
				JSONObject json = JSONObject.parseObject(returnResult.getJsonStr());
				JSONArray jsonArray = json.getJSONArray("ReturnTable");
				List<Menu> list = Menu.getMenus(jsonArray.toJSONString());
				result.setData(list);
				Cache.set(user.getUsercode()+"_"+session.getId(), result, "30mn");
			}else{
				result.setCode(AppConfig.ERROR);
				result.setMessage(HelpUtils.getErrorMessage(returnResult.getJsonStr()));
			}
			renderJSON(result);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			throw new JSONReqException(e.getMessage());
		}
	}
	
	/**
	 * 得到统计信息
	 */
	public static void getStaticsData(){
		User user = getLoginUser();
		Map<String, List<JSONObject>> staticsData = new HashMap<String, List<JSONObject>>();
		Result result = new Result();
		String jsonData = InJsonData.getRequsetJsonData(AppConfig.HANDLE_TYPE, AppConfig.PLAT_GETUPCOMINGINFO, user.getUsercode(), AppConfig.CURRENT_PLAT, user.getDbName(), request.remoteAddress, "");
		try{
			ReturnResult returnResult = WebServiceHelpUtil.execute(jsonData);
			if(returnResult.isSuccess()){
				result.setCode(AppConfig.SUCCESS);
				JSONObject json = JSONObject.parseObject(returnResult.getJsonStr());
				JSONArray jsonArray = json.getJSONArray("ReturnTable");
				for(int i=0; i<jsonArray.size(); i++){
					JSONObject jsonObject = jsonArray.getJSONObject(i);
					String key = jsonObject.getString("UpcomingSortName");
					List<JSONObject> list = staticsData.get(key);
					if(list==null){
						list = new ArrayList<JSONObject>();
						staticsData.put(key, list);
					}
					list.add(jsonObject);
				}
				result.setData(staticsData);
			}else{
				result.setCode(AppConfig.ERROR);
				result.setMessage(returnResult.getJsonStr());
			}
		}catch(Exception e){
			result.setCode(AppConfig.ERROR);
			result.setMessage(e.getMessage());
		}
		renderJSON(result);
	}
}
